﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using ExamAnalysis.Core;
using ExamAnalysis.Model;
using ExamAnalysis.Core.Statistics;
using System.Xml;
using System.Xml.Xsl;

namespace ExamAnalysis.Reports
{
    public partial class CompareReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    this.ClassStore.DataSource = db.ClassInfo
                        .Where(p => p.SchoolID == Config.CurrentSchoolID)
                        .ToList();
                    this.ClassStore.DataBind();

                    this.CourseStore.DataSource = db.CourseInfo.ToList();
                    this.CourseStore.DataBind();

                    BindExamList();
                }
            }
        }
        [DirectMethod]
        public void CourseList_Changed(object sender, DirectEventArgs e)
        {
            BindExamList(Convert.ToInt32(CourseList.SelectedItem.Value));
        }

        private void BindExamList()
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                this.ExamStore.DataSource = db.ExamInfo
                         .OrderByDescending(p => p.ExamID)
                         .Select(p => new { p.ExamID, p.ExamName, p.CalendarID, p.SchoolCalender.DisplayName });
                this.ExamStore.DataBind();
            }
        }

        private void BindExamList(int courseID)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                this.ExamStore.DataSource = db.ExamInfo
                        .Where(p => p.ExamInCourse.Where(q => q.CourseID == courseID).Count() > 0)
                        .OrderByDescending(p => p.ExamID)
                        .Select(p => new { p.ExamID, p.ExamName, p.CalendarID, p.SchoolCalender.DisplayName });
                this.ExamStore.DataBind();
            }
        }

        [DirectMethod]
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                if (ClassList.SelectedItem == null)
                {
                    X.Msg.Alert("通知", "请选择班级！").Show();
                }
                else
                {
                    List<int> examIDs = new List<int>();
                    foreach (var item in (ExamGrid.GetSelectionModel() as RowSelectionModel).SelectedRows)
                    {
                        examIDs.Add(Convert.ToInt32(item.RecordID));
                    }
                    var result = Report.ProcessCompareReport(Convert.ToInt32(ClassList.SelectedItem.Value.ToString()), examIDs);
                    GridStore.DataSource = result;
                    GridStore.DataBind();
                    GridChartStore.DataSource = result;
                    GridChartStore.DataBind();
                }
            }
        }
        protected void Store_Submit(object sender, StoreSubmitDataEventArgs e)
        {
            string format = this.FormatType.Value.ToString();

            XmlNode xml = e.Xml;

            this.Response.Clear();

            switch (format)
            {
                case "xml":
                    string strXml = xml.OuterXml;
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xml");
                    this.Response.AddHeader("Content-Length", strXml.Length.ToString());
                    this.Response.ContentType = "application/xml";
                    this.Response.Write(strXml);
                    break;

                case "xls":
                    this.Response.ContentType = "application/vnd.ms-excel";
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
                    XslCompiledTransform xtExcel = new XslCompiledTransform();
                    xtExcel.Load(Server.MapPath("~/Reports/Export/Excel.xsl"));
                    xtExcel.Transform(xml, null, Response.OutputStream);

                    break;

                case "csv":
                    this.Response.ContentType = "application/octet-stream";
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.csv");
                    XslCompiledTransform xtCsv = new XslCompiledTransform();
                    xtCsv.Load(Server.MapPath("~/Reports/Export/Csv.xsl"));
                    xtCsv.Transform(xml, null, Response.OutputStream);

                    break;
            }
            this.Response.End();
        }
    }
}